﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    public enum Heroname//枚举英雄
    {
        刘备=1,
        孙权=2,
        曹操=3
    }
    public enum Chuquaning//枚举出拳
    {
        剪刀 = 1,
        石头 = 2,
        布 = 3
    }
    class Program
    {
        static void Main(string[] args)//主方法
        {
            Play();
        }
        public static void Play()
        {
            Caipan cp = new Caipan();//实例化裁判
            cp.RuleSpeak();//引入裁判规则
            //while (true)
            //{
            Console.WriteLine("请选择对方角色<1:刘备 2:孙权 3:曹操>");
            int heroname = int.Parse(Console.ReadLine());
            Heroname he = (Heroname)Enum.Parse(typeof(Heroname), heroname.ToString());
                //if (heroname.Equals(he.))
                //{
                    Console.WriteLine("请输入您的名字：");
                    string username = Console.ReadLine();

                    Console.WriteLine("{0} vs {1} 对战", username, he);
                    Console.WriteLine("开始游戏吗？<Y/N>");
                    string yn = Console.ReadLine();
                    YN(yn, username, he);
                //}
                //else
                //{
                //    Console.WriteLine("输入英雄不存在，请重新输入！");
                //}
            //}
        }
        public static void Youxi(string name,Heroname he)
        {
            Caipan cb = new Caipan();

            //我方出拳
            Console.WriteLine("请出拳：1.剪刀 2.石头 3.布（输入相应数字：）");
            int renchuquan = int.Parse(Console.ReadLine());
            Chuquaning cqr = (Chuquaning)Enum.Parse(typeof(Chuquaning), renchuquan.ToString());

            //电脑随机出拳
            int computercq = GetRandomByGuid(0, 3);
            Chuquaning cqc = (Chuquaning)Enum.Parse(typeof(Chuquaning), computercq.ToString());

            //比较双方出拳结果
            Bijiao(renchuquan,computercq,cb,name,he,cqc,cqr);
        }
        public static void Bijiao(int renchuquan, int computercq, Caipan cb, string name, Heroname he, Chuquaning cqc, Chuquaning cqr)
        {
            int time = 0;
            int rscore = 0;
            int cscore = 0;
            while (true)
            {
                time++;
                switch (renchuquan)
                {
                    case 1:
                        Console.WriteLine($"{name}：出拳：{cqr}");
                        Console.WriteLine($"{he}：出拳：{cqc}");
                        qinkuan1(renchuquan, computercq, cb, name, rscore, cscore);
                        break;
                    case 2:
                        Console.WriteLine($"{name}:出拳：{cqr}");
                        Console.WriteLine($"{he}：出拳：{cqc}");
                        qinkuan2(renchuquan, computercq, cb, name, rscore, cscore);
                        break;
                    case 3:
                        Console.WriteLine($"{name}：出拳：{cqr}");
                        Console.WriteLine($"{he}：出拳：{cqc}");
                        qinkuan3(renchuquan, computercq, cb, name, rscore, cscore);
                        break;
                    default:
                        Console.WriteLine("出拳错误，请重新输入！");
                        break;
                }
                
                Console.WriteLine("是否开始下一轮？<Y/N>");
                string b = Console.ReadLine();

                YNyn(b, name, he, rscore, cscore, time);
            }
        }
        private static void qinkuan3(int aa,int bb,Caipan cb,string name,int rscore,int cscore)
        {
            if (aa == bb)
            {
                cb.He();
            }
            else if (aa != bb)
            {
                if (aa == 1)
                {
                    cb.Shibai(name);
                    cscore++;
                }
                else if (aa == 2)
                {
                    cb.Win(name);
                    rscore++;
                }
            }
            else
            {
                Console.WriteLine("输入错误！");
            }
        }
        private static void qinkuan2(int aa, int bb, Caipan cb,string name, int rscore, int cscore)
        {
            if (aa == bb)
            {
                cb.He();
            }
            else if (aa != bb)
            {
                if (aa == 1)
                {
                    cb.Win(name);
                    rscore ++;
                }
                else if (aa == 3)
                {
                    cb.Shibai(name);
                    cscore++;
                }
            }
            else
            {
                Console.WriteLine("输入错误！");
            }
        }
        private static void qinkuan1(int ren, int cpu, Caipan cb, string name, int rscore, int cscore)
        {
            if (ren == cpu)
            {
                cb.He();
            }
            else if (ren != cpu)
            {
                if (cpu == 2)
                {
                    cb.Shibai(name);
                    cscore ++;
                }
                else if (cpu == 3)
                {
                    cb.Win(name);
                    rscore++;
                }
            }
            else
            {
                Console.WriteLine("输入错误！");
            }
        }
        static void Return(string name,Heroname he,int cishu,int rfs,int cfs)
        {
            Caipan cb = new Caipan();
            cb.returnup(name,he,cishu,rfs,cfs);

            Console.WriteLine("要开始下一局吗？<Y/N>");
            string xzxz = Console.ReadLine();
            YN(xzxz, name, he);
        }

        private static void YN(string beginorend, string name, Heroname he)
        {
            if (beginorend == "y")
            {
                Youxi(name, he);
            }
            else if (beginorend == "Y")
            {
                Youxi(name, he);
            }
            else if (beginorend == "n")
            {
                Play();
            }
            else if (beginorend == "N")
            {
                Play();
            }
            else
            {
                Console.WriteLine("系统退出!");
                Environment.Exit(0);
            }
        }
        private static void YNyn(string xz,string name, Heroname h,int rfengshu,int cfengshu,int time)
        {
            if (xz == "y")
            {
                Youxi(name,h);
            }
            else if (xz == "Y")
            {
                Youxi(name,h);
            }
            else if (xz == "n")
            {
                Return(name,h, time, rfengshu, cfengshu);
            }
            else if (xz == "N")
            {
                Return(name,h, time, rfengshu, cfengshu);
            }
            else
            {
                Console.WriteLine("系统退出！");
                Environment.Exit(0);
            }
        }
        //电脑出拳
        private static int GetRandomByGuid(int min, int max)
        {
            Random ro = new Random(GetRandomSeedByGuid());
            int ronum = ro.Next(min, max);
            return ronum;
        }
        static int GetRandomSeedByGuid()
        {
            return new Guid().GetHashCode();
        }
    }
}